package com.cff.shorturl.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static final String FullDatePattern = "yyyy-MM-dd HH:mm:ss";
	public static final String SimpleDatePatternNoSysmbol = "yyyyMMddHHmmss";
	public static final String SimpleDatePattern = "yyyyMMdd";
	public static final String SimpleTimePattern = "HHmmss";
	public static final String CurrentDatePattern = "yyyy-MM-dd";
	public static final String ChinaDatePattern = "MM月dd日";
	public static final String SimpleMonthPattern = "yyyyMM";
	/**
	 * 获取两个日期之间所有的月份
	 * 
	 * @param minDate
	 * @param maxDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getMonthBetween(Date minDate, Date maxDate, String format) throws ParseException {
		List<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat(format);// 格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(minDate);
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

		max.setTime(maxDate);
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}

		return result;
	}

	/**
	 * 根据日期获得前一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date prevDate(Date date) {
		if (null == date) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setLenient(false);
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);
		return calendar.getTime();
	}
	
	/**
	 * 根据日期获得前几天
	 * 
	 * @param date
	 * @return
	 */
	public static Date prevDate(Date date,int num) {
		if (null == date) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setLenient(false);
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -num);
		return calendar.getTime();
	}

	/**
	 * 获取前一天，时间为00:00:00
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date prevDateNotime(Date date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setLenient(false);
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);

		String formatDate = format(calendar.getTime(), CurrentDatePattern);
		DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format2.parse(formatDate + " 00:00:00");
	}
	
	/**
	 * 获取前一天，时间为23:59:59
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date prevDateFulltime(Date date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setLenient(false);
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);

		String formatDate = format(calendar.getTime(), CurrentDatePattern);
		DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format2.parse(formatDate + " 23:59:59");
	}

	/**
	 * 获取后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date nextDate(Date date) {
		if (null == date) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setLenient(false);
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		return calendar.getTime();
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 * 
	 * @return String
	 */
	public static String format(Date date, String pattern) {
		return date == null ? "" : new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 使用参数Format将字符串转为Date
	 * 
	 * @return Date
	 */
	public static Date parse(String strDate, String pattern) throws ParseException {
		if (strDate == null || strDate.trim().length() < 1)
			return null;
		return new SimpleDateFormat(pattern).parse(strDate);
	}

	/**
	 * 获取上个月的日期
	 * 
	 * @return
	 */
	public static Date getLatMothDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		return calendar.getTime();
	}

	/**
	 * 指定日期加上天数后的日期
	 * 
	 * @param date
	 *            创建时间
	 * @param num
	 *            为增加的天数
	 * @return
	 * @throws ParseException
	 */
	public static Date plusDay(Date date, int num) {
		if(num == 0)return date;
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.DATE, num);
		Date currdate = ca.getTime();
		return currdate;
	}

	/**
	 * date2比date1多的天数
	 * 
	 * @param date1
	 *            早
	 * @param date2
	 *            晚
	 * @throws IllegalArgumentException
	 *             时间先后顺序不对
	 * @return
	 */
	public static int differentDays(Date date1, Date date2) {
		if (date1.after(date2)) {
			throw new IllegalArgumentException("时间先后顺序不对!");
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2) // 同一年
		{
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
				{
					timeDistance += 366;
				} else // 不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2 - day1);
		} else // 不同年
		{
			System.out.println("判断day2 - day1 : " + (day2 - day1));
			return day2 - day1;
		}
	}
	
	/**
	 * date2比date1多的月份
	 * 
	 * @param date1
	 *            早
	 * @param date2
	 *            晚
	 * @throws IllegalArgumentException
	 *             时间先后顺序不对
	 * @return
	 */
	public static int differentMonth(Date date1, Date date2) {
		if (date1.after(date2)) {
			throw new IllegalArgumentException("时间先后顺序不对!");
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1 = cal1.get(Calendar.MONTH);
		int day2 = cal2.get(Calendar.MONTH);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2) // 同一年
		{
			int timeDistance = 0;
			timeDistance = (year2 - year1) * 12;

			return timeDistance + (day2 - day1);
		} else // 不同年
		{
			return day2 - day1;
		}
	}
	
	/**
	 * 比较两个日期
	 * @param basicDate
	 * @param compareDate
	 * @return true if <code>compareDate >= basicDate</code>, else false
	 */
	public static boolean compareDate(Date basicDate, Date compareDate) {
		if (null == basicDate || null == compareDate) {
			throw new IllegalArgumentException("The date must not be null.");
		}
		Calendar basicCalendar = Calendar.getInstance();
		Calendar compareCalendar = Calendar.getInstance();
		basicCalendar.setTime(basicDate);
		compareCalendar.setTime(compareDate);
		return compareDate(basicCalendar, compareCalendar);
	}
	/**
	 * 比较两个日期
	 * @param basicCalendar
	 * @param compareCalendar
	 * @return true if <code>compareDate >= basicDate</code>, else false
	 */
	public static boolean compareDate(Calendar basicCalendar, Calendar compareCalendar) {
		if (null == basicCalendar || null == compareCalendar) {
			throw new IllegalArgumentException("The date must not be null.");
		}
		if (basicCalendar.get(Calendar.ERA) == compareCalendar.get(Calendar.ERA)) {
			if (basicCalendar.get(Calendar.YEAR) == compareCalendar.get(Calendar.YEAR)) {
				return (basicCalendar.get(Calendar.DAY_OF_YEAR) <= compareCalendar.get(Calendar.DAY_OF_YEAR));
			}
			return (basicCalendar.get(Calendar.YEAR) < compareCalendar.get(Calendar.YEAR));
		}
		return (basicCalendar.get(Calendar.ERA) < compareCalendar.get(Calendar.ERA));
	}
}
